#include "ExportExcel.h"
#include <wx/file.h>
#define SQL_CONDITION wxT("where 1=1")

CExportExcel::CExportExcel(void)
{
}

CExportExcel::~CExportExcel(void)
{
}

void CExportExcel::SetDBManager(CDBWrapper *dbwrapper)
{
	dbWrapper = dbwrapper;
}

void CExportExcel::ExportTuxClient()
{
/*
	int i = 0, nArrayCount = 0;
	TuxedoClientArray *tuxcltarray;
	TuxedoClient tuxclt;

	wxString headname_list[]= {
		wxT("Record Time"),
		wxT("Client Name"),
		wxT("Client PID"),
		wxT("Client Address"),
		wxT("Client Status"),
		wxT("Client Connection Period"),
		wxT("")
	};

	tuxcltarray = dbWrapper->TuxCltLstQuery(SQL_CONDITION);
	nArrayCount = tuxcltarray->GetCount();
	wxFile *clt = new wxFile();

	clt->Create("C:\\ClientList.xls", true);
	clt->Open("C:\\ClientList.xls", wxFile::write );
	for ( i = 0; !headname_list[i].IsEmpty();  i++) {
		clt->Write(headname_list[i]);
		clt->Write( wxT("\t") );
	}
	clt->Write( wxT("\n") );
	for( i = 0;i < nArrayCount; i++ ) {
		tuxclt = tuxcltarray->Item(i);
		clt->Write( tuxclt.RecTime + wxT("\t") );
		clt->Write( tuxclt.ClientName + wxT("\t") );
		clt->Write( tuxclt.ClientPID + wxT("\t") );
		clt->Write( tuxclt.ClientAddr + wxT("\t") );
		clt->Write( tuxclt.Status + wxT("\t") );
		clt->Write( tuxclt.ConnectTime + wxT("\t") );
		clt->Write( wxT("\n") );
	}
	clt->Close();

	objExcel.CallMethod(wxT("Workbooks.Open"), wxT("C:\\ClientList.xls") );
	
	//wxRemoveFile();
*/
	return;
}

void CExportExcel::ExportTuxQueue()
{
#if 0
	int i = 0, nArrayCount = 0;
	TuxedoQueueArray *tuxquearray;
	TuxedoQueue tuxque;

	wxString headname_list[]= {
		wxT("Record Time"),
		wxT("Prog Name"),
		wxT("Queued"),
		wxT("Server Count"),
		wxT("IPCS ID"),
		wxT("")
	};

	tuxquearray = dbWrapper->TuxQueLstQuery(SQL_CONDITION);
	nArrayCount = tuxquearray->GetCount();
	wxFile *clt = new wxFile();

	clt->Create("C:\\QueueList.xls", true);
	clt->Open("C:\\QueueList.xls", wxFile::write );
	for ( i = 0; !headname_list[i].IsEmpty();  i++) {
		clt->Write(headname_list[i]);
		clt->Write( wxT("\t") );
	}
	clt->Write( wxT("\n") );
	for( i = 0;i < nArrayCount; i++ ) {
		tuxque = tuxquearray->Item(i);
		clt->Write( tuxque.RecTime + wxT("\t") );
		clt->Write( tuxque.ProgName + wxT("\t") );
		clt->Write( tuxque.Queued + wxT("\t") );
		clt->Write( tuxque.SvrCnt + wxT("\t") );
		clt->Write( tuxque.QueName + wxT("\t") );
		clt->Write( wxT("\n") );
	}
	clt->Close();

	objExcel.CallMethod(wxT("Workbooks.Open"), wxT("C:\\QueueList.xls") );
#endif
	return;
}

void CExportExcel::ExportTuxServer()
{
	int i = 0, nArrayCount = 0;
	TuxedoServerArray *tuxsvrarray;
	TuxedoServer tuxsvr;

	wxString headname_list[]= {
		wxT("Record Time"),
		wxT("Prog Name"),
		wxT("Memory Use"),
		wxT("Current Svc"),
		wxT("Svr MIN"),
		wxT("Svr MAX"),
		wxT("RqDone"),
		wxT("Process ID"),
		wxT("Queue Name"),
		wxT("")
	};

	tuxsvrarray = dbWrapper->TuxSvrLstQuery(SQL_CONDITION);
	nArrayCount = tuxsvrarray->GetCount();
	wxFile *clt = new wxFile();

	clt->Create("C:\\ServerList.xls", true);
	clt->Open("C:\\ServerList.xls", wxFile::write );
	for ( i = 0; !headname_list[i].IsEmpty();  i++) {
		clt->Write(headname_list[i]);
		clt->Write( wxT("\t") );
	}
	clt->Write( wxT("\n") );
	for( i = 0;i < nArrayCount; i++ ) {
		tuxsvr = tuxsvrarray->Item(i);
		clt->Write( tuxsvr.RecTime + wxT("\t") );
		clt->Write( tuxsvr.ProgName + wxT("\t") );
		clt->Write( tuxsvr.UseMem + wxT("\t") );
		clt->Write( tuxsvr.CurrService + wxT("\t") );
		clt->Write( tuxsvr.SvrMin + wxT("\t") );
		clt->Write( tuxsvr.SvrMax + wxT("\t") );
		clt->Write( tuxsvr.RqDone + wxT("\t") );
		clt->Write( tuxsvr.ProcessID + wxT("\t") );
		clt->Write( tuxsvr.QueName + wxT("\t") );
		clt->Write( wxT("\n") );
	}
	clt->Close();

	objExcel.CallMethod(wxT("Workbooks.Open"), wxT("C:\\ServerList.xls") );

	return;
}

int CExportExcel::CreateExcelFile()
{
	wxString sheetname_list[]= {
		wxT("Clients"),
		wxT("Queues"),
		wxT("Servers"),
		wxT("")
	};

	wxVariant appo;
	wxAutomationObject sheet;
	wxAutomationObject range;
	wxVariant rng[1];

	if ( objExcel.CreateInstance("Excel.Application") ){
		appo=objExcel.PutProperty("visible", true);
		ExportTuxClient();
		//appo=objExcel.CallMethod("Workbooks.Add");
		ExportTuxQueue();
		ExportTuxServer();
		return 1;
	} 
	//objExcel->CallMethod("Sheets.Add");
	//appo=xlbook.GetProperty("Sheets.Count"); 
/*
	for (int i = 0; i< 3; i++ ) {
		rng[0] = wxVariant((long) (i+1)); 
		objExcel.GetObject(sheet, wxT("WorkSheets"), 1, rng);
		appo = sheet.PutProperty(wxT("Name"),sheetname_list[i] );
	}	
*/	

//	appo=objExcel.CallMethod("Workbooks.Add");


	return 1;
}